package com.lanou.myapp.funny.picturefunny;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.circlepicture.MyCircle;
import com.lanou.myapp.imageloader.VolleySingleton;
import com.lanou.myapp.circlepicture.Circlelmage;
import com.squareup.picasso.Picasso;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by dllo on 16/1/13.
 */
public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.MyViewHolder> {
    private Context context;
    private List<PictureFunnyBeans.ListEntity> data;

    public PictureAdapter(Context context) {
        this.context = context;

    }

    public void addData(List<PictureFunnyBeans.ListEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder;
        View view = LayoutInflater.from(context).inflate(R.layout.item_picture_funny, null);
        myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.circlelmage, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getU().getHeader().get(1), listener, 200, 200);
//        Glide.with(context).load(data.get(position).getU().getHeader().get(1)).into(holder.circlelmage);

        if (data.get(position).getGif() != null) {
//            Picasso.with(context).load(data.get(position).getGif().getImages().get(0)).fit().centerCrop().into(holder.networkImageView);
            Glide.with(context).load(data.get(position).getGif().getImages().get(0)).into(holder.networkImageView);
//            Picasso.with(context).load(data.get(position).getGif().getImages().get(0)).into(holder.networkImageView);
        }

        if (data.get(position).getImage() != null) {
            ImageLoader.ImageListener listener2 = ImageLoader.getImageListener(holder.networkImageView, R.mipmap.v6_boy_unseletor_icon, R.mipmap.v6_boy_unseletor_icon);
            VolleySingleton.getImageLoader().get(data.get(position).getImage().getMedium().get(0), listener2,400,300);

//            holder.networkImageView.setDefaultImageResId(R.mipmap.v6_boy_unseletor_icon);
//            holder.networkImageView.setErrorImageResId(R.mipmap.v5_icon_usercenter);
//            holder.networkImageView.setImageUrl(data.get(position).getImage().getMedium().get(0), imageLoader);
            // imageview
//            Glide.with(context).load(data.get(position).getImage().getMedium().get(0)).into(holder.networkImageView);

        }


        holder.tv_author.setText(data.get(position).getU().getName());
        holder.tv_time.setText(data.get(position).getPasstime());
        holder.tv_content.setText(data.get(position).getText());
        holder.tv_tag1.setText(data.get(position).getTags().get(0).getName());

        if (data.get(position).getTags().size() > 1) {
            holder.tv_tag2.setText(data.get(position).getTags().get(1).getName());
        }

        if (data.get(position).getTags().size() > 2) {
            holder.tv_tag3.setText(data.get(position).getTags().get(2).getName());
        }
        holder.tv_num1.setText(data.get(position).getUp() + "");
        holder.tv_num2.setText(data.get(position).getDown() + "");
        holder.tv_num3.setText(data.get(position).getForward() + "");
        holder.tv_num4.setText(data.get(position).getComment() + "");
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size()/3;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_author, tv_time, tv_content;
        private TextView tv_tag1, tv_tag2, tv_tag3;
        private TextView tv_num1, tv_num2, tv_num3, tv_num4;
        private CircleImageView circlelmage;
        private ImageView networkImageView;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_author = (TextView) itemView.findViewById(R.id.tv_name_picture_funny);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time_picture_funny);
            tv_content = (TextView) itemView.findViewById(R.id.tv_title_picture_funny);
            tv_tag1 = (TextView) itemView.findViewById(R.id.tv_tag_picture_funny);
            tv_tag2 = (TextView) itemView.findViewById(R.id.tv_tag2_picture_funny);
            tv_tag3 = (TextView) itemView.findViewById(R.id.tv_tag3_picture_funny);
            tv_num1 = (TextView) itemView.findViewById(R.id.tv_num_zan);
            tv_num2 = (TextView) itemView.findViewById(R.id.tv_num_picture_funny_cai);
            tv_num3 = (TextView) itemView.findViewById(R.id.tv_num_picture_funny_share);
            tv_num4 = (TextView) itemView.findViewById(R.id.tv_num_picture_funny_comment);
            networkImageView = (ImageView) itemView.findViewById(R.id.iv_gif);
            circlelmage = (CircleImageView) itemView.findViewById(R.id.iv_item_picture_funny);

        }
    }
}
